var JS_ADDEMP = function () {
    var owner = this;
    owner.currentId = null;
    owner.dom_tag = "addemp";
    owner.dom_edit = $("#" + owner.dom_tag + "_div_add");
    owner.dom_loading = $("#" + owner.dom_tag + "_div_loading");
    owner.listBoxTag = new Array();
    owner.listBoxJob = new Array();
    owner.listBoxInterviewer = new Array();
    owner.listBoxHr = new Array();
    owner.fileUrl = "";
    $("#" + owner.dom_tag + "_ipt_getType").combobox({
        onSelect: function (record) {
            if (record.value == "内推" || record.value == "外推") {
                $("#" + owner.dom_tag + "_ipt_referee").combobox("select", "推荐");
            }
        }
    });
    $("#" + owner.dom_tag + "_ipt_tip").tagbox({
        onChange: function () {
            $("#" + owner.dom_tag + "_ipt_tip").combobox("loadData", owner.listBoxTag);
            $("#" + owner.dom_tag + "_ipt_interviewTime").Zebra_DatePicker({});
        },
        onAddTag: function (value) {
            owner.listBoxTag.push({
                value: value,
                text: value
            });
            $("#" + owner.dom_tag + "_ipt_tip").combobox("loadData", owner.listBoxTag);
            $.ajax({
                type: "post",
                url: "/tag/addTag",
                cache: false,
                dataType: "json",
                data: {
                    type: "default",
                    name: value
                }
            });
        }
    });

    owner.init = function () {
        window.setTimeout(function () {
            $("#" + owner.dom_tag + "_ipt_interviewTime").Zebra_DatePicker();
        }, 1000);
        owner.loadTagData();
        owner.loadJobData();
        owner.loadUserData();
        owner.currentId = request("id");
        if(owner.currentId!=null) {
            $("#" + owner.dom_tag + "_ipt_inviteStatus").combobox("disable");
            $("#" + owner.dom_tag + "_ipt_interviewStatus").combobox("disable");
            $("#" + owner.dom_tag + "_ipt_file").filebox("disable");
            owner.loadEmployee(owner.currentId);
        }
    };

    owner.loadTagData = function () {
        $.ajax({
            type: "get",
            url: "/tag/getNamesByType",
            cache: false,
            dataType: "json",
            data: {
                type: "default"
            }
        }).done(function (result) {
            if (result.success) {
                for (var i = 0; i < result.data.length; i++) {
                    owner.listBoxTag.push({
                        value: result.data[i],
                        text: result.data[i]
                    });
                }
                $("#" + owner.dom_tag + "_ipt_tip").tagbox("loadData", owner.listBoxTag);
            } else {
                $.messager.alert("提示信息", result.message);
            }
        });
    };

    owner.loadJobData = function () {
        $.ajax({
            type: "get",
            url: "/job/getForList",
            cache: false,
            dataType: "json",
            data: {}
        }).done(function (result) {
            if (result.success) {
                for (var i = 0; i < result.data.length; i++) {
                    var obj = result.data[i];
                    owner.listBoxJob.push({
                        value: obj.id,
                        text: obj.dep1Name + "_" + obj.dep2Name + "_" + obj.name
                    });
                }
                $("#" + owner.dom_tag + "_ipt_job").combobox("loadData", owner.listBoxJob);

            } else {
                $.messager.alert("提示信息", result.message);
            }
        });
    };

    owner.loadUserData = function () {
        $.ajax({
            type: "get",
            url: "/user/getForList",
            cache: false,
            dataType: "json",
            data: {}
        }).done(function (result) {
            if (result.success) {
                for (var i = 0; i < result.data.length; i++) {
                    var obj = result.data[i];
                    if (obj.type == "interviewer") {
                        owner.listBoxInterviewer.push({
                            value: obj.id,
                            text: obj.name
                        });
                    } else {
                        owner.listBoxHr.push({
                            value: obj.id,
                            text: obj.name
                        });
                    }
                }
                $("#" + owner.dom_tag + "_ipt_interviewer1").combobox("loadData", owner.listBoxInterviewer);
                $("#" + owner.dom_tag + "_ipt_interviewer2").combobox("loadData", owner.listBoxInterviewer);
                $("#" + owner.dom_tag + "_ipt_interviewer0").combobox("loadData", owner.listBoxHr);
            } else {
                $.messager.alert("提示信息", result.message);
            }
        });
    };

    owner.upload = function(jobId) {
        var dtd = $.Deferred();
        if ($("#" + owner.dom_tag + "_ipt_file").filebox("getValue")==null) {
            owner.fileUrl = "";
            dtd.resolve();
        }
        $.upload({
            url: "/employ/upload",
            domId: $("#" + owner.dom_tag + "_ipt_file").data("id"),
            dataType: 'html',
            params: {
                dir: jobId
            },
            onComplate: function (result) {
                if (result.success) {
                    owner.fileUrl = result.data;
                    dtd.resolve();
                } else {
                    $.messager.alert("提示信息", result.message);
                    dtd.reject();
                }
            },
            onError: function () {
                $.messager.alert("提示信息", "网络繁忙，请稍后再试！");
                dtd.reject();
            }
        });
        return dtd.promise();
    };

    owner.addEmp = function () {
        owner.dom_loading.window("open").window("center");
        var isAllValid = new Array();
        owner.dom_edit.find(".easyui-textbox").each(function () {
            if (!$(this).textbox("isValid")) {
                isAllValid.push($(this).prev("label").html());
            }
        });
        owner.dom_edit.find(".easyui-tagbox").each(function () {
            if (!$(this).tagbox("isValid")) {
                isAllValid.push($(this).prev("label").html());
            }
        });
        owner.dom_edit.find(".easyui-combobox").each(function () {
            if (!$(this).combobox("isValid")) {
                isAllValid.push($(this).prev("label").html());
            }
        });
        if (isAllValid.length > 0) {
            $.messager.alert("提示信息", isAllValid.join(",") + " 输入不正确！");
            owner.dom_loading.window("close");
            return;
        }
        if (owner.currentId!=null) {
            owner.edit();
            return;
        }
        $.when(owner.upload($("#" + owner.dom_tag + "_ipt_job").combobox("getValue"))).done(function () {
            $.ajax({
                type: "post",
                url: "/employ/add",
                cache: false,
                dataType: "json",
                data: {
                    "name": $("#" + owner.dom_tag + "_ipt_name").textbox("getValue"),
                    "gender": $("#" + owner.dom_tag + "_ipt_gender").combobox("getValue"),
                    "age": $("#" + owner.dom_tag + "_ipt_age").numberspinner("getValue"),
                    "workAge": $("#" + owner.dom_tag + "_ipt_workAge").numberspinner("getValue"),
                    "phone": $("#" + owner.dom_tag + "_ipt_phone").textbox("getValue"),
                    "mail": $("#" + owner.dom_tag + "_ipt_mail").textbox("getValue"),
                    "pipe": $("#" + owner.dom_tag + "_ipt_getType").combobox("getValue"),
                    "referee": $("#" + owner.dom_tag + "_ipt_referee").combobox("getValue"),
                    "sender": $("#" + owner.dom_tag + "_ipt_sender").textbox("getValue"),
                    "tips": $("#" + owner.dom_tag + "_ipt_tip").tagbox("getValue"),
                    "jobId": $("#" + owner.dom_tag + "_ipt_job").combobox("getValue"),
                    "jobName": $("#" + owner.dom_tag + "_ipt_job").combobox("getText"),
                    "inviteStatus": $("#" + owner.dom_tag + "_ipt_inviteStatus").combobox("getValue"),
                    "interviewStatus": $("#" + owner.dom_tag + "_ipt_interviewStatus").combobox("getValue"),
                    "interviewTime": $("#" + owner.dom_tag + "_ipt_interviewTime").val(),
                    "interviewType": $("#" + owner.dom_tag + "_ipt_interviewType").combobox("getValue"),
                    "interviewer0": $("#" + owner.dom_tag + "_ipt_interviewer0").combobox("getValue"),
                    "interviewer1": $("#" + owner.dom_tag + "_ipt_interviewer1").combobox("getValue"),
                    "interviewer2": $("#" + owner.dom_tag + "_ipt_interviewer2").combobox("getValue"),
                    "fileUrl": owner.fileUrl
                }
            }).done(function (result) {
                if (result.success) {
                    $.messager.alert("提示信息", "添加成功！");
                    owner.reset();
                } else {
                    $.messager.alert("提示信息", result.message);
                }
            }).fail(function () {
                $.messager.alert("提示信息", "网络繁忙，请稍后再试！");
            });
        }).always(function () {
            owner.dom_loading.window("close");
        });
    };

    owner.reset = function () {
        owner.dom_edit.find(".easyui-textbox").textbox("clear")
            .end().find(".easyui-combobox").combobox("reset")
            .end().find(".easyui-tagbox").tagbox("clear")
            .end().find(".easyui-numberspinner").numberspinner("reset")
            .end().find(".easyui-filebox").filebox("clear")
            .end().find(".textbox").val("");
    };

    owner.loadEmployee = function (id) {
        owner.dom_loading.window("open").window("center");
        $.ajax({
            type: "get",
            url: "/employ/get",
            cache: false,
            dataType: "json",
            data: {
                "id": id
            }
        }).done(function (result) {
            if (result.success) {
                owner.resetEdit(result.data);
            } else {
                $.messager.alert("提示信息", result.message);
            }
        }).fail(function () {
            $.messager.alert("提示信息", "网络繁忙，请稍后再试！");
        }).complete(function () {
            owner.dom_loading.window("close");
        });
    };

    owner.resetEdit = function (item) {
        $("#" + owner.dom_tag + "_ipt_name").textbox("setValue", item.name);
        $("#" + owner.dom_tag + "_ipt_gender").combobox("setValue", item.gender);
        $("#" + owner.dom_tag + "_ipt_age").numberspinner("setValue", item.age);
        $("#" + owner.dom_tag + "_ipt_workAge").numberspinner("setValue", item.workAge);
        $("#" + owner.dom_tag + "_ipt_phone").textbox("setValue", item.phone);
        $("#" + owner.dom_tag + "_ipt_mail").textbox("setValue", item.mail);
        $("#" + owner.dom_tag + "_ipt_getType").combobox("setValue", item.pipe);
        $("#" + owner.dom_tag + "_ipt_referee").combobox("setValue", item.referee);
        $("#" + owner.dom_tag + "_ipt_sender").textbox("setValue", item.sender);
        $("#" + owner.dom_tag + "_ipt_tip").tagbox("setValue", item.tips);
        $("#" + owner.dom_tag + "_ipt_job").combobox("setValue", item.jobId);
        $("#" + owner.dom_tag + "_ipt_inviteStatus").combobox("setValue", item.inviteStatus);
        $("#" + owner.dom_tag + "_ipt_interviewStatus").combobox("setValue", item.interviewStatus);
        $("#" + owner.dom_tag + "_ipt_interviewTime").val(item.interviewTime);
        $("#" + owner.dom_tag + "_ipt_interviewType").combobox("setValue", item.interviewType);
        $("#" + owner.dom_tag + "_ipt_interviewer1").combobox("setValue", item.interviewer1);
        $("#" + owner.dom_tag + "_ipt_interviewer2").combobox("setValue", item.interviewer2);
        $("#" + owner.dom_tag + "_ipt_interviewer0").combobox("setValue", item.interviewer0);
    };

    owner.edit = function () {
        $.ajax({
            type: "post",
            url: "/employ/edit",
            cache: false,
            dataType: "json",
            data: {
                "id": owner.currentId,
                "name": $("#" + owner.dom_tag + "_ipt_name").textbox("getValue"),
                "gender": $("#" + owner.dom_tag + "_ipt_gender").combobox("getValue"),
                "age": $("#" + owner.dom_tag + "_ipt_age").numberspinner("getValue"),
                "workAge": $("#" + owner.dom_tag + "_ipt_workAge").numberspinner("getValue"),
                "phone": $("#" + owner.dom_tag + "_ipt_phone").textbox("getValue"),
                "mail": $("#" + owner.dom_tag + "_ipt_mail").textbox("getValue"),
                "pipe": $("#" + owner.dom_tag + "_ipt_getType").combobox("getValue"),
                "referee": $("#" + owner.dom_tag + "_ipt_referee").combobox("getValue"),
                "sender": $("#" + owner.dom_tag + "_ipt_sender").textbox("getValue"),
                "tips": $("#" + owner.dom_tag + "_ipt_tip").tagbox("getValue"),
                "jobId": $("#" + owner.dom_tag + "_ipt_job").combobox("getValue"),
                "jobName": $("#" + owner.dom_tag + "_ipt_job").combobox("getText"),
                "interviewTime": $("#" + owner.dom_tag + "_ipt_interviewTime").val(),
                "interviewType": $("#" + owner.dom_tag + "_ipt_interviewType").combobox("getValue"),
                "interviewer0": $("#" + owner.dom_tag + "_ipt_interviewer0").combobox("getValue"),
                "interviewer1": $("#" + owner.dom_tag + "_ipt_interviewer1").combobox("getValue"),
                "interviewer2": $("#" + owner.dom_tag + "_ipt_interviewer2").combobox("getValue")
            }
        }).done(function (result) {
            if (result.success) {
                $.messager.alert("提示信息", "编辑成功！");
            } else {
                $.messager.alert("提示信息", result.message);
            }
        }).fail(function () {
            $.messager.alert("提示信息", "网络繁忙，请稍后再试！");
        }).complete(function () {
            owner.dom_loading.window("close");
        });
    };


};